package cn.groupmarket.test;

import cn.groupmarket.api.IMarketTradeService;
import cn.groupmarket.api.dto.LockMarketPayOrderRequestDTO;
import cn.groupmarket.api.dto.LockMarketPayOrderResponseDTO;
import cn.groupmarket.api.response.Response;
import cn.groupmarket.domain.activity.model.entity.MarketProductEntity;
import cn.groupmarket.domain.activity.model.entity.TrialBalanceEntity;
import cn.groupmarket.domain.activity.service.IIndexGroupBuyMarketService;
import cn.groupmarket.domain.tag.service.ITagService;
import cn.groupmarket.domain.trade.model.entity.MarketPayOrderEntity;
import cn.groupmarket.domain.trade.model.valobj.GroupBuyProgressVO;
import cn.groupmarket.domain.trade.service.ITradeOrderService;
import cn.groupmarket.infrastructure.dao.IGroupBuyActivityDao;
import cn.groupmarket.infrastructure.dao.IGroupBuyDiscountDao;
import cn.groupmarket.infrastructure.dao.po.GroupBuyActivity;
import cn.groupmarket.infrastructure.dao.po.GroupBuyDiscount;
import cn.groupmarket.infrastructure.redis.IRedisService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RBitSet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DaoTest {
    @Resource
    private IGroupBuyActivityDao groupBuyActivityDao;

    @Resource
    private IGroupBuyDiscountDao groupBuyDiscountDao;

    @Resource
    private IIndexGroupBuyMarketService indexGroupBuyMarketService;

    @Resource
    private ITagService tagService;

    @Resource
    private IRedisService redisService;

    @Resource
    private ITradeOrderService tradeOrderService;

    @Resource
    private IMarketTradeService marketTradeService;

    public static void main(String[] args) {
        SpringApplication.run(DaoTest.class, args);
    }
    @Test
    public void test(){
        List<GroupBuyActivity> groupBuyActivityList = groupBuyActivityDao.queryGroupBuyActivityList();
        for(GroupBuyActivity groupBuyActivity : groupBuyActivityList){
            log.info(groupBuyActivity.toString());
        }
    }

    @Test
    public void test1(){
        List<GroupBuyDiscount> groupBuyDiscountList = groupBuyDiscountDao.queryGroupBuyDiscountList();
        for(GroupBuyDiscount groupBuyDiscount : groupBuyDiscountList){
            log.info("{}", JSON.toJSONString(groupBuyDiscount));
        }
    }

    @Test
    public void test2() throws Exception {
        MarketProductEntity marketProductEntity = new MarketProductEntity();
        marketProductEntity.setUserId("yangge");
        marketProductEntity.setSource("s01");
        marketProductEntity.setChannel("c01");
        marketProductEntity.setProductId("9890001");

        TrialBalanceEntity trialBalanceEntity = indexGroupBuyMarketService.indexMarketTrial(marketProductEntity);
        log.info("请求参数{}", JSON.toJSONString(marketProductEntity));
        log.info("返回结果:{}", JSON.toJSONString(trialBalanceEntity));
    }
    @Test
    public void test3() {
        tagService.execTagBatchJob("RQ_KJHKL98UU78H66554GFDV","10002");
    }
    @Test
    public void test4() {
        RBitSet bitSet = redisService.getBitSet("RQ_KJHKL98UU78H66554GFDV");
//        log.info("测试结果:{}", bitSet.get(redisService.getIndexFromUserId("liergou")));
        log.info("测试结果:{}", bitSet.get(redisService.getIndexFromUserId("yg1")));
    }
    @Test
    public void test5() {
        MarketProductEntity marketProductEntity = new MarketProductEntity();
        marketProductEntity.setUserId("yannnn");
    }

    @Test
    public void test6() {
        GroupBuyProgressVO groupBuyProgressVO = tradeOrderService.queryGroupBuyProgress("64451200");
        log.info("测试结果{}", JSON.toJSONString(groupBuyProgressVO));
    }

    @Test
    public void test7() {
        MarketPayOrderEntity marketPayOrderEntity = tradeOrderService.queryNoPayMarketPayOrderByOutTradeNo("054537636261", "xiaofuge");
        log.info("测试结果{}", JSON.toJSONString(marketPayOrderEntity));
    }

    @Test
    public void test8() {
        LockMarketPayOrderRequestDTO lockMarketPayOrderRequestDTO  = new LockMarketPayOrderRequestDTO();
        lockMarketPayOrderRequestDTO.setUserId("yg1");
        lockMarketPayOrderRequestDTO.setTeamId(null);
        lockMarketPayOrderRequestDTO.setActivityId(100123L);
        lockMarketPayOrderRequestDTO.setGoodsId("9890001");
        lockMarketPayOrderRequestDTO.setSource("s01");
        lockMarketPayOrderRequestDTO.setChannel("c01");
        lockMarketPayOrderRequestDTO.setOutTradeNo(RandomStringUtils.randomNumeric(12));

        Response<LockMarketPayOrderResponseDTO> lockMarketPayOrderResponseDTOResponse = marketTradeService.lockMarketPayOrder(lockMarketPayOrderRequestDTO);

        log.info("测试结果 req{} res{}",JSON.toJSONString(lockMarketPayOrderRequestDTO),JSON.toJSONString(lockMarketPayOrderResponseDTOResponse));

    }
    @Test
    public void test9() {
        LockMarketPayOrderRequestDTO lockMarketPayOrderRequestDTO  = new LockMarketPayOrderRequestDTO();
        lockMarketPayOrderRequestDTO.setUserId("yangge");
        lockMarketPayOrderRequestDTO.setTeamId("40361826");
        lockMarketPayOrderRequestDTO.setActivityId(100123L);
        lockMarketPayOrderRequestDTO.setGoodsId("9890001");
        lockMarketPayOrderRequestDTO.setSource("s01");
        lockMarketPayOrderRequestDTO.setChannel("c01");
        lockMarketPayOrderRequestDTO.setOutTradeNo(RandomStringUtils.randomNumeric(12));

        Response<LockMarketPayOrderResponseDTO> lockMarketPayOrderResponseDTOResponse = marketTradeService.lockMarketPayOrder(lockMarketPayOrderRequestDTO);

        log.info("测试结果 req{} res{}",JSON.toJSONString(lockMarketPayOrderRequestDTO),JSON.toJSONString(lockMarketPayOrderResponseDTOResponse));

    }
}
